'''
等待
    等待页面的加载完成
    -sleep(seconds) 
        休眠指定的时间，单位是秒
    -implicitly_wait(seconds) 
        隐式等待,参数是最大等待时间，单位是秒
        隐式等待实际上是等待页面加载完成
        隐式等待语句对它以后的代码都有效
        隐式等待并不总是有效，此时可以使用sleep()
    -WebDriverWait 
        显示等待，等待指定的元素加载完成，或者其它条件
'''
from selenium import webdriver
from time import sleep,time
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

try:
    start = time()
    driver = webdriver.Chrome()
    driver.get('http://localhost/ranzhi/www/sys/user-login.html')
    driver.maximize_window()
    # 隐式等待
    driver.implicitly_wait(10)

    # 登陆

    # driver.find_element_by_id('account').send_keys('admin')
    # 显示等待
    element = WebDriverWait(driver,5,0.5).until(EC.presence_of_element_located((By.ID,'account')))
    element.send_keys('admin')

    driver.find_element_by_id('password').send_keys('123456')
    driver.find_element_by_id('submit').click()

    # sleep(1)
    # 点击后台管理
    driver.find_element_by_id('s-menu-superadmin').click()
    # sleep(1)

    # 定位iframe元素
    iframe = driver.find_element_by_id('iframe-superadmin')
    # 切换到iframe中
    driver.switch_to.frame(iframe)
    # 点击 添加成员
    driver.find_element_by_partial_link_text('添加成员').click()
    # 用户名
    driver.find_element_by_id('account').send_keys('tom')

    # 真实姓名
    driver.find_element_by_id('realname').send_keys('Tom Cruse')

    # 性别
    driver.find_element_by_id('genderm').click()

    # 部门
    select = driver.find_element_by_id('dept')
    depts = Select(select)
    depts.select_by_index(2) # 根据下标进行选择，下标从0开始
    depts.select_by_value('10') # 根据value属性的值来进行选择
    depts.select_by_visible_text('/开发部') # 根据可见文本来进行选择

    # 角色
    select = driver.find_element_by_id('role')
    roles = Select(select)
    roles.select_by_value('pm')

    # 密码
    driver.find_element_by_id('password1').send_keys('123456')

    # 密码
    driver.find_element_by_id('password2').send_keys('123456')

    # 邮箱
    driver.find_element_by_id('email').send_keys('tom@163.com')

    # 保存
    driver.find_element_by_id('submit').click()
    # sleep(2)

    # 删除
    # driver.find_element_by_xpath('/html/body/div/div/div/div[2]/div/div/table/tbody/tr[2]/td[11]/a[3]').click()
    # sleep(1)
    # 点击 取消
    # driver.switch_to.alert.dismiss()
    # 删除
    # driver.find_element_by_xpath('/html/body/div/div/div/div[2]/div/div/table/tbody/tr[2]/td[11]/a[3]').click()
    # sleep(1)
    # 点击 确定
    # driver.switch_to.alert.accept()

except Exception as e:
    print(e)
finally:
    # sleep(2)
    driver.close()
    end = time()
    print('耗时=%s'%(end-start))